package com.yungu.swift.api.route.v1.driver;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.api.utils.FileUploadUtil;
import com.yungu.swift.api.utils.RandomUtil;
import com.yungu.swift.assets.driver.model.dto.CarDto;
import com.yungu.swift.assets.driver.model.dto.DriverChangeCarDto;
import com.yungu.swift.assets.driver.model.dto.DriverDto;
import com.yungu.swift.assets.driver.model.dto.DriverFaceRecognitionDto;
import com.yungu.swift.assets.driver.service.CarService;
import com.yungu.swift.assets.driver.service.DriverChangeCarService;
import com.yungu.swift.assets.driver.service.DriverFaceRecognitionService;
import com.yungu.swift.assets.driver.service.DriverService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.common.model.dto.CommonArticleDto;
import com.yungu.swift.common.model.dto.CommonErrorMessageDto;
import com.yungu.swift.common.model.dto.CommonTagDto;
import com.yungu.swift.common.model.vo.AppVersionVo;
import com.yungu.swift.common.model.vo.PrivacyPhoneVo;
import com.yungu.swift.common.service.*;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.constants.MagicConstant;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import com.yungu.swift.utils.ValidateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Zerrion
 * @description
 * @date 2016/12/6. <br>
 */
@Controller
@RequestMapping("/api/v1/driver/change")
@Api(value = "【司机】更换车辆相关接口", tags = "司机更换车辆相关接口")
public class DriChangeCarApi extends BaseApi {
    @Reference
    private DriverChangeCarService driverChangeCarService;
    @Reference
    private DriverService driverService;
    @Reference
    private CarService carService;
    @Reference
    private CommonErrorMessageService commonErrorMessageService;
    @Reference
    private DriverFaceRecognitionService driverFaceRecognitionService;
    @Reference
    private CommonSmsService commonSmsService;
    @Reference
    private CommonPrivacyPhoneService commonPrivacyPhoneService;

    @ResponseBody
    @RequestMapping(value = {"/detail"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "获取当前司机是否有未审核和被驳回的记录", response = ResponseData.class, notes = "list")
    public ResponseData<DriverChangeCarDto> detail(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "uuid", value = "driverUUid", required = true) @RequestParam String uuid
    ) {
        //获取当前司机未审核和被驳回的记录
        Map<String, Object> map = MapUtils.build(4);
        map.put("driverUUid", uuid);
        map.put("isNotice", 0);
        ResponseData<List<DriverChangeCarDto>> list = driverChangeCarService.list(map);
        if (list.isSuccess() && CollectionUtils.isNotEmpty(list.getData())) {
            return ResponseData.buildSuccessResponse("获取成功", list.getData().get(0));
        } else if (list.isSuccess() && CollectionUtils.isEmpty(list.getData())) {
            //获取当前司机信息
            ResponseData<DriverDto> driverDtoResponseData = driverService.get(uuid);
            //获取当前司机绑定的车辆信息
            if (driverDtoResponseData.isSuccess() && driverDtoResponseData.getData().getCarUuid() != null) {
                ResponseData<CarDto> carDtoResponseData = carService.get(driverDtoResponseData.getData().getCarUuid());
                return ResponseData.buildSuccessResponse("获取成功", new DriverChangeCarDto(driverDtoResponseData.getData(), carDtoResponseData.getData()));
            } else if (driverDtoResponseData.isSuccess() && driverDtoResponseData.getData().getCarUuid() == null) {
                return ResponseData.buildSuccessResponse("获取成功", new DriverChangeCarDto(driverDtoResponseData.getData(), new CarDto()));
            }
        }
        return null;
    }

    @RequestMapping(value = {"/insert"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "添加司机更换车辆记录", response = ResponseData.class, notes = "list")
    public ResponseData<Integer> insert(@RequestBody @Validated DriverChangeCarDto driverChangeCarDto) {
        if (driverChangeCarDto.getUuid() == null || driverChangeCarDto.getUuid().equals("")) {
            driverChangeCarDto.setUuid(StringUtils.buildUUID());
            return driverChangeCarService.insert(driverChangeCarDto);
        } else {
            driverChangeCarDto.setStatus(1);
            if (driverChangeCarService.edit(driverChangeCarDto).getData()) {
                return ResponseData.buildSuccessResponse(1);
            } else {
                return ResponseData.buildErrorResponse(500, "修改失败");
            }
        }
    }

    @RequestMapping(value = {"/update"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "修改通知状态", response = ResponseData.class, notes = "list")
    public ResponseData<Boolean> listOfCancelMsg(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "uuid", value = "uuid", required = true) @RequestParam String uuid
    ) {
        DriverChangeCarDto driverChangeCarDto = new DriverChangeCarDto();
        driverChangeCarDto.setUuid(uuid);
        driverChangeCarDto.setIsNotice(1);
        return driverChangeCarService.edit(driverChangeCarDto);
    }

    @RequestMapping(value = {"/phone"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "获取隐私号码接口", response = ResponseData.class, notes = "list")
    public ResponseData<String> phone(@RequestBody @Validated PrivacyPhoneVo privacyPhoneVo) throws Exception {
        return commonPrivacyPhoneService.getPrivacyPhone(privacyPhoneVo);
    }

}
